package oleg.locale;


/**
 * Copyright 2008,2009 Oleg Frenkel
 *
 * This file is part of PerfectSearch.
 *
 * PerfectSearch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PerfectSearch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PerfectSearch.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.ByteArrayInputStream;
import javax.microedition.lcdui.Image;
import javaxx.microedition.global.ResourceManager;

import oleg.C;
import oleg.MainForm;
import oleg.cfg.Config;
import oleg.log.Logger;
import oleg.rms.RMSManager;
import oleg.util.Arrays;

public class LocaleManager
{
  static private LocaleManager instance = new LocaleManager();

  private ResourceManager res = null;

  private String[] allLocaleNames = null;
  private RMSManager rmsMgr = RMSManager.instance();

  // This is a singleton
  private LocaleManager() {}

  static public LocaleManager instance() {
    return instance;
  }

  private ResourceManager getResrcMgr(String locale) {
    if (res == null || !res.getLocale().equals(locale)) {
      try {
        res = ResourceManager.getManager("properties", locale);
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }

    return res;
  }

  public String[] getAllLocaleNames() {
    if (allLocaleNames == null) {
      allLocaleNames = new String[] {"en"};
      try {
        allLocaleNames = ResourceManager.getSupportedLocales("properties");
//         Logger.log("LocalManager.getAllLocaleNames: " +
//                    " successfully read locale names, length: " + 
//                    allLocaleNames.length);
        Arrays.sort(allLocaleNames);
      } catch (Exception ex) {
        Logger.log("LocalManager.getAllLocaleNames: " + ex.getMessage());
        ex.printStackTrace();
      }
    }

    return allLocaleNames;
  } 

  // Use current locale
  public String getProperty(int name) {
    ResourceManager resMgr = getResrcMgr(Config.instance().getInterfaceLang());
    return resMgr != null ? resMgr.getString(name) : C.LOCALE_STR[name];
  }

  // Return property value according to locale
  public String getProperty(String locale, int name) {
    try {
      return ResourceManager.getManager("properties", locale).getString(name);
    } catch (Exception ex) {
      ex.printStackTrace();
    }

    return getProperty(name);
  }

  public Image getImage(int imageId) {
    try {
      byte[] imageData = ResourceManager.getManager("images", "").getData(imageId);
      return Image.createImage(new ByteArrayInputStream(imageData));
    } catch (Exception ex) {
      Logger.log("LocaleManager.getImage(): " + ex.getMessage());
    }

    return null;
  }

}